<!DOCTYPE html>

<html lang="en">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width">
	<title>Common Modules Source: control/MousePosition.js</title>

	<!--[if lt IE 9]>
	<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
	<![endif]-->
	<link type="text/css" rel="stylesheet" href="styles/sunlight.default.css">

	<link type="text/css" rel="stylesheet" href="styles/site.simplex.css">

</head>

<body>

<div class="navbar navbar-default navbar-fixed-top navbar-inverse">
<div class="container">
	<div class="navbar-header">
		<a class="navbar-brand" href="index.html">Common Modules</a>
		<button class="navbar-toggle" type="button" data-toggle="collapse" data-target="#topNavigation">
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
			<span class="icon-bar"></span>
        </button>
	</div>
	<div class="navbar-collapse collapse" id="topNavigation">
		<ul class="nav navbar-nav">
			
			<li class="dropdown">
				<a href="classes.list.html" class="dropdown-toggle" data-toggle="dropdown">Classes<b class="caret"></b></a>
				<ul class="dropdown-menu ">
					<li><a href="Base.html">Base</a></li><li><a href="Event.html">Event</a></li><li><a href="Html.html">Html</a></li><li><a href="Internal.html">Internal</a></li><li><a href="ol.Overlay.Popup.html">ol.Overlay.Popup</a></li>
				</ul>
			</li>
			
			<li class="dropdown">
				<a href="global.html" class="dropdown-toggle" data-toggle="dropdown">Global<b class="caret"></b></a>
				<ul class="dropdown-menu ">
					<li><a href="global.html#addClass">addClass</a></li><li><a href="global.html#addListener">addListener</a></li><li><a href="global.html#arrayRemove">arrayRemove</a></li><li><a href="global.html#clear">clear</a></li><li><a href="global.html#create">create</a></li><li><a href="global.html#createHidden">createHidden</a></li><li><a href="global.html#empty">empty</a></li><li><a href="global.html#EventType">EventType</a></li><li><a href="global.html#findListener">findListener</a></li><li><a href="global.html#firstUpperToCase">firstUpperToCase</a></li><li><a href="global.html#get">get</a></li><li><a href="global.html#getChildByTagName">getChildByTagName</a></li><li><a href="global.html#getClass">getClass</a></li><li><a href="global.html#getDomEventKey">getDomEventKey</a></li><li><a href="global.html#getElementsByClassName">getElementsByClassName</a></li><li><a href="global.html#getListeners">getListeners</a></li><li><a href="global.html#getrandom">getrandom</a></li><li><a href="global.html#getStyle">getStyle</a></li><li><a href="global.html#getuuid">getuuid</a></li><li><a href="global.html#hasClass">hasClass</a></li><li><a href="global.html#isObject">isObject</a></li><li><a href="global.html#isString">isString</a></li><li><a href="global.html#layerConfigDefautls">layerConfigDefautls</a></li><li><a href="global.html#listen">listen</a></li><li><a href="global.html#listenOnce">listenOnce</a></li><li><a href="global.html#logo">logo</a></li><li><a href="global.html#OL_Style_Type">OL_Style_Type</a></li><li><a href="global.html#remove">remove</a></li><li><a href="global.html#removeClass">removeClass</a></li><li><a href="global.html#removeListener">removeListener</a></li><li><a href="global.html#removeListeners">removeListeners</a></li><li><a href="global.html#setClass">setClass</a></li><li><a href="global.html#splitWords">splitWords</a></li><li><a href="global.html#stamp">stamp</a></li><li><a href="global.html#toBack">toBack</a></li><li><a href="global.html#toFront">toFront</a></li><li><a href="global.html#trim">trim</a></li><li><a href="global.html#unListen">unListen</a></li><li><a href="global.html#unlistenAll">unlistenAll</a></li><li><a href="global.html#unListenByKey">unListenByKey</a></li><li><a href="global.html#upperFirstChart">upperFirstChart</a></li>
				</ul>
			</li>
			
		</ul>
        
            <div class="col-sm-3 col-md-3">
                <form class="navbar-form" role="search">
                    <div class="input-group">
                        <input type="text" class="form-control" placeholder="Search" name="q" id="search-input">
                        <div class="input-group-btn">
                            <button class="btn btn-default" id="search-submit"><i class="glyphicon glyphicon-search"></i></button>
                        </div>
                    </div>
                </form>
            </div>
        
	</div>

</div>
</div>


<div class="container" id="toc-content">
<div class="row">

	
	<div class="col-md-12">
	
		<div id="main">
			

		<h1 class="page-title">Source: control/MousePosition.js</h1>
    
<section>
    <article>
        <pre
            class="sunlight-highlight-javascript linenums">/**
 * @desc 坐标实时拾取
 */
import { ol } from '../constants'
import '../asset/scss/mousePosition.scss'
import {BASE_CLASS_NAME} from '../constants'
import * as htmlUtils from '../utils/domUtils'
import * as Events from '../event/Events'
ol.control.MousePositionH = function (options = {}) {
  this.className_ = options.className !== undefined ? options.className : 'gmap-mouse-position'
  let element = htmlUtils.create('div', (this.className_ + ' ' + BASE_CLASS_NAME.CLASS_UNSELECTABLE))
  let render = options.render ? options.render : ol.control.MousePositionH.render
  ol.control.Control.call(this, {
    element: element,
    render: render,
    target: options.target
  })

  Events.listen(this, 'change:' + ol.control.MousePositionH.Property_.PROJECTION, this.handleProjectionChanged_, this)

  if (options.coordinateFormat) {
    this.setCoordinateFormat(options.coordinateFormat)
  }
  if (options.projection) {
    this.setProjection(options.projection)
  }

  if (options.units) {
    this.setUnits(options.units)
  }
  /**
   * @private
   * @type {string}
   */
  this.undefinedHTML_ = options.undefinedHTML !== undefined ? options.undefinedHTML : ''

  /**
   * @private
   * @type {string}
   */
  this.renderedHTML_ = element.innerHTML

  /**
   * @private
   * @type {ol.proj.Projection}
   */
  this.mapProjection_ = null

  /**
   * @private
   * @type {?ol.TransformFunction}
   */
  this.transform_ = null

  /**
   * @private
   * @type {ol.Pixel}
   */
  this.lastMouseMovePixel_ = null

  /**
   * 是否显示跟随气泡（同步开启点击锚坐标）
   * @type {boolean}
   * @private
   */
  this.followMouse_ = options['followMouse'] === true ? options['followMouse'] : false

  /**
   * 当前气泡
   * @type {null}
   * @private
   */
  this.popver_ = null

  /**
   * 容器边界
   * @type {null}
   * @private
   */
  this._bounds = null
}

ol.inherits(ol.control.MousePositionH, ol.control.Control)

/**
 * 更新控件元素
 * @param mapEvent
 */
ol.control.MousePositionH.render = function (mapEvent) {
  let frameState = mapEvent.frameState
  if (!frameState) {
    this.mapProjection_ = null
  } else {
    if (this.mapProjection_ !== frameState.viewState.projection) {
      this.mapProjection_ = frameState.viewState.projection
      this.transform_ = null
    }
  }
  if (this.getMap() &amp;&amp; this.lastMouseMovePixel_) {
    if (this.followMouse_) {
      this.followMousePopver_(this.lastMouseMovePixel_)
    } else {
      this.updateHTML_(this.lastMouseMovePixel_)
    }
  }
}

/**
 * 处理投影变化事件
 * @private
 */
ol.control.MousePositionH.prototype.handleProjectionChanged_ = function () {
  this.transform_ = null
}

/**
 * 获取投影转换函数
 * @returns {ol.CoordinateFormatType|undefined}
 */
ol.control.MousePositionH.prototype.getCoordinateFormat = function () {
  return (this.get(ol.control.MousePositionH.Property_.COORDINATE_FORMAT))
}

/**
 * 获取投影
 * @returns {ol.proj.Projection|undefined}
 */
ol.control.MousePositionH.prototype.getProjection = function () {
  return (this.get(ol.control.MousePositionH.Property_.PROJECTION))
}

/**
 * 处理鼠标移动事件
 * @param event
 */
ol.control.MousePositionH.prototype.handleMouseMove = function (event) {
  let map = this.getMap()
  if (map) {
    this.lastMouseMovePixel_ = map.getEventPixel(event)
    if (this.lastMouseMovePixel_) {
      if (this.followMouse_) {
        this.followMousePopver_(event)
      } else {
        this.updateHTML_(this.lastMouseMovePixel_)
      }
    }
  }
}

/**
 * 显示气泡
 * @param event
 * @private
 */
ol.control.MousePositionH.prototype.followMousePopver_ = function () {
  let html = this.getHTML_(this.lastMouseMovePixel_)
  let map = this.getMap()
  let coordinates = map.getCoordinateFromPixel(this.lastMouseMovePixel_)
  if (!this.popver_) {
    let ele = htmlUtils.create('div', this.className_ + '_overlay')
    ele.innerHTML = html
    this.popver_ = new ol.Overlay({
      element: ele,
      offset: [10, 0],
      position: coordinates,
      positioning: 'center-left'
    })
    map.addOverlay(this.popver_)
    map.render()
  } else {
    let _ele = this.popver_.getElement()
    _ele.innerHTML = html
    if (_ele.offsetWidth >= this._bounds.width - this.lastMouseMovePixel_[0]) {
      this.popver_.setPositioning('center-right')
      this.popver_.setOffset([-10, 0])
    } else {
      this.popver_.setPositioning('center-left')
      this.popver_.setOffset([10, 0])
    }
    this.popver_.setPosition(coordinates)
    this.popver_.setElement(_ele)
  }
}

/**
 * 处理鼠标移出视图事件
 * @param event
 */
ol.control.MousePositionH.prototype.handleMouseOut = function (event) {
  this.updateHTML_(null)
  this.lastMouseMovePixel_ = null
}

/**
 * setMap
 * @param map
 */
ol.control.MousePositionH.prototype.setMap = function (map) {
  ol.control.Control.prototype.setMap.call(this, map)
  if (map) {
    let viewport = map.getViewport()
    this._bounds = map.getTargetElement().getBoundingClientRect()
    Events.listen(viewport, 'mousemove', this.handleMouseMove, this)
    Events.listen(viewport, 'mouseout', this.handleMouseOut, this)
  }
}

/**
 * 设置坐标格式化函数
 * @param format
 */
ol.control.MousePositionH.prototype.setCoordinateFormat = function (format) {
  this.set(ol.control.MousePositionH.Property_.COORDINATE_FORMAT, format)
}

/**
 * 设置投影转换
 * @param projection
 */
ol.control.MousePositionH.prototype.setProjection = function (projection) {
  this.set(ol.control.MousePositionH.Property_.PROJECTION, ol.proj.get(projection))
}

/**
 * 设置显示单位
 * @param units
 */
ol.control.MousePositionH.prototype.setUnits = function (units) {
  this.set(ol.control.MousePositionH.Property_.PROJECTION, units)
}

/**
 * 更新页面控件
 * @param pixel
 * @private
 */
ol.control.MousePositionH.prototype.updateHTML_ = function (pixel) {
  let html = this.getHTML_(pixel)
  if (!this.renderedHTML_ || html !== this.renderedHTML_) {
    this.element.innerHTML = html
    this.renderedHTML_ = html
  }
}

/**
 * 获取dom
 * @param pixel
 * @returns {string|*}
 * @private
 */
ol.control.MousePositionH.prototype.getHTML_ = function (pixel) {
  let html = this.undefinedHTML_
  if (pixel &amp;&amp; this.mapProjection_) {
    if (!this.transform_) {
      let projection = this.getProjection()
      if (projection) {
        this.transform_ = ol.proj.getTransformFromProjections(
          this.mapProjection_, projection)
      } else {
        this.transform_ = ol.control.MousePositionH.identityTransform
      }
    }
    let map = this.getMap()
    let coordinate = map.getCoordinateFromPixel(pixel)
    if (coordinate) {
      this.transform_(coordinate, coordinate)
      let coordinateFormat = this.getCoordinateFormat()
      if (coordinateFormat) {
        html = coordinateFormat(coordinate)
      } else {
        html = ol.control.MousePositionH.Property_.UNITS[0] + '：' + coordinate[0] +
          ' ' + ol.control.MousePositionH.Property_.UNITS[1] + '：' + coordinate[1]
      }
    }
  }
  return html
}

/**
 * 属性
 * @type {{PROJECTION: string, COORDINATE_FORMAT: string}}
 * @private
 */
ol.control.MousePositionH.Property_ = {
  PROJECTION: 'projection',
  COORDINATE_FORMAT: 'coordinateFormat',
  UNITS: ['经度', '纬度']
}

/**
 * 转换
 * @param input
 * @param output
 * @param dimension
 * @returns {*}
 */
ol.control.MousePositionH.identityTransform = function (input, output, dimension) {
  if (output !== undefined &amp;&amp; input !== output) {
    for (let i = 0, ii = input.length; i &lt; ii; ++i) {
      output[i] = input[i]
    }
    input = output
  }
  return input
}

const olControlMousePosition = ol.control.MousePositionH
export default olControlMousePosition
</pre>
    </article>
</section>





		</div>
	</div>

	<div class="clearfix"></div>

	

</div>
</div>


    <div class="modal fade" id="searchResults">
      <div class="modal-dialog">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">Search results</h4>
          </div>
          <div class="modal-body"></div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div><!-- /.modal-content -->
      </div><!-- /.modal-dialog -->
    </div>


<footer>


	<span class="copyright">
	https://lzw.me.
	</span>

<span class="jsdoc-message">
	Documentation generated by <a href="https://github.com/jsdoc3/jsdoc">JSDoc 3.5.5</a>
	
		on Sun Aug 12th 2018
	
	using the <a href="https://github.com/docstrap/docstrap">DocStrap template</a>.
</span>
</footer>

<script src="scripts/docstrap.lib.js"></script>
<script src="scripts/toc.js"></script>

    <script type="text/javascript" src="scripts/fulltext-search-ui.js"></script>


<script>
$( function () {
	$( "[id*='$']" ).each( function () {
		var $this = $( this );

		$this.attr( "id", $this.attr( "id" ).replace( "$", "__" ) );
	} );

	$( ".tutorial-section pre, .readme-section pre, pre.prettyprint.source" ).each( function () {
		var $this = $( this );

		var example = $this.find( "code" );
		exampleText = example.html();
		var lang = /{@lang (.*?)}/.exec( exampleText );
		if ( lang && lang[1] ) {
			exampleText = exampleText.replace( lang[0], "" );
			example.html( exampleText );
			lang = lang[1];
		} else {
			var langClassMatch = example.parent()[0].className.match(/lang\-(\S+)/);
			lang = langClassMatch ? langClassMatch[1] : "javascript";
		}

		if ( lang ) {

			$this
			.addClass( "sunlight-highlight-" + lang )
			.addClass( "linenums" )
			.html( example.html() );

		}
	} );

	Sunlight.highlightAll( {
		lineNumbers : true,
		showMenu : true,
		enableDoclinks : true
	} );

	$.catchAnchorLinks( {
        navbarOffset: 10
	} );
	$( "#toc" ).toc( {
		anchorName  : function ( i, heading, prefix ) {
			return $( heading ).attr( "id" ) || ( prefix + i );
		},
		selectors   : "#toc-content h1,#toc-content h2,#toc-content h3,#toc-content h4",
		showAndHide : false,
		smoothScrolling: true
	} );

	$( "#main span[id^='toc']" ).addClass( "toc-shim" );
	$( '.dropdown-toggle' ).dropdown();

    $( "table" ).each( function () {
      var $this = $( this );
      $this.addClass('table');
    } );

} );
</script>



<!--Navigation and Symbol Display-->


<!--Google Analytics-->



    <script type="text/javascript">
        $(document).ready(function() {
            SearcherDisplay.init();
        });
    </script>


</body>
</html>
